/* eslint-disable no-case-declarations */
/* eslint-disable no-param-reassign */
/* eslint-disable default-case */
/* eslint-disable guard-for-in */
/* eslint-disable no-restricted-syntax */
import { message } from 'antd';
import {
  getInsSearchList,
  getInjuryList,
  getInjuryPageList,
  deleteInjuryMul,
  updateInjuryStatus,
  getBigName,
  custName,
  submitEnable,
  submitAll,
  fetchInjuryDetails,
  getDetailInfo,
  injuryUpdate,
  getFileNo,
} from '@/services/bigaccomanage';

export default {
  state: {
    ratioList: [],
    ratioListDetail: [],
    activeKey: '1',
    tabList: [
      { itemCode: '1', itemName: '社保' },
      { itemCode: '2', itemName: '公积金' },
      { itemCode: '3', itemName: '工伤二级户' },
    ],
    formFieldsValue: {},
    expandForm: false,
    bigwareList: [],
    injuryList: [],
    selectedRows: [],
    selectedRowKeys: [],
    pageData: {
      // 列表数据
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    bigResult: [],
    comList: [],
    yesNoFlag: [],
    insAccoName: '',
    detailInfo: {},
    detailFormFieldsValue: {},
  },

  effects: {
    *getFileNo({ payload }, { call, put }) {
      const res = yield call(getFileNo, payload);
      if (res.success) {
        const { data } = res;
        if (data) {
          yield put({
            type: 'handleFileNo',
            payload: data,
          });
        } else {
          message.warning('没有文件');
        }
      }
    },
    *editCustName({ payload }, { call, put }) {
      const res = yield call(custName, payload);
      if (res.success) {
        yield put({
          type: 'updateEditCustName',
          payload: res.data,
        });
      } else {
        message.warning(res.message || '服务端错误');
      }
    },
    *injuryUpdate({ payload }, { call }) {
      const res = yield call(injuryUpdate, payload);
      return res;
    },
    *getDetailInfo({ payload }, { call, put }) {
      const res = yield call(getDetailInfo, payload);
      if (res.success) {
        yield put({
          type: 'handleDetailInfo',
          payload: res.data,
        });
      } else {
        message.warning(res.message || '服务端错误');
      }
      return res;
    },
    *fetchInjuryDetails({ payload }, { call, put }) {
      const res = yield call(fetchInjuryDetails, payload);
      if (res.success) {
        yield put({
          type: 'handleDetails',
          payload: res.data,
        });
      } else {
        message.warning(res.message || '服务端错误');
      }
    },
    *submitAll({ payload }, { call }) {
      const res = yield call(submitAll, payload);
      return res;
    },
    *submitEnable({ payload }, { call }) {
      const res = yield call(submitEnable, payload);
      return res;
    },
    *custName({ payload }, { call, put }) {
      const res = yield call(custName, payload);
      if (res.success) {
        yield put({
          type: 'updateModelData',
          payload: {
            comList: res.data,
          },
        });
      } else {
        message.warning(res.message || '服务端错误');
      }
    },
    *getBigName({ payload }, { call, put }) {
      const res = yield call(getBigName, payload);
      if (res.success) {
        yield put({
          type: 'updateModelData',
          payload: {
            bigResult: res.data,
          },
        });
      } else {
        message.warning(res.message || '服务端错误');
      }
      return res;
    },
    // 获取大库查询列表
    *fetchBigwareList({ payload }, { call, put }) {
      const response = yield call(getInsSearchList, payload);
      if (response.success) {
        yield put({
          type: 'getBigwareList',
          payload: response.data,
        });
      } else {
        message.warning(response.message);
      }
    },
    // 获取工伤二级户查询列表
    *fetchInjuryList({ payload }, { call, put }) {
      const response = yield call(getInjuryList, payload);
      if (response.success) {
        yield put({
          type: 'getInjuryList',
          payload: response.data,
        });
      } else {
        message.warning(response.message);
      }
    },
    *fetchInjuryPageList({ payload }, { call, put }) {
      const response = yield call(getInjuryPageList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      if (response.success) {
        yield put({
          type: 'getInjuryPageList',
          payload: { ...response, currentPage, pageSize },
        });
      } else {
        message.warning(response.message);
      }
    },
    *deleteInjury({ payload }, { call, put, select }) {
      const response = yield call(deleteInjuryMul, payload);

      if (response.success) {
        const formFieldsValue = yield select(state => state.injuryware.formFieldsValue);
        const current = yield select(state => state.injuryware.pageData.pagination.current);
        const pageSize = yield select(state => state.injuryware.pageData.pagination.pageSize);
        yield put({
          type: 'fetchInjuryPageList',
          payload: {
            ...formFieldsValue,
            mdCustId: formFieldsValue.mdCustId?.key,
            pageArg: {
              currentPage: current,
              pageSize,
            },
          },
        });
        message.success(`${response.message || '删除成功！'}`);
      } else {
        message.warning(`${response.message || response.data || '删除失败！'}`);
      }
    },
    *updateStatus({ payload }, { call, put, select }) {
      const warnMessage = Number(payload.state) === 2 ? '停用成功' : '启用成功';
      const { injSecAccoId } = payload;
      const response = yield call(updateInjuryStatus, { injSecAccoId });
      if (response.success) {
        const formFieldsValue = yield select(state => state.injuryware.formFieldsValue);
        const current = yield select(state => state.injuryware.pageData.pagination.current);
        const pageSize = yield select(state => state.injuryware.pageData.pagination.pageSize);
        yield put({
          type: 'fetchInjuryPageList',
          payload: {
            injSecAcco: {
              injSecAccoCode:
                formFieldsValue.injSecAccoCode && formFieldsValue.injSecAccoCode.trim(),
              state: formFieldsValue.state && formFieldsValue.state.trim(),
              insAccoId: formFieldsValue.injInsAccoId && formFieldsValue.injInsAccoId.trim(),
              injSecAccoId: formFieldsValue.injSecAccoId && formFieldsValue.injSecAccoId.trim(),
            },
            mdCustId: formFieldsValue.mdCustId?.key,
            pageArg: {
              currentPage: current,
              pageSize,
            },
          },
        });
        message.success(`${warnMessage}`);
      } else {
        message.warning(`${response.message || '修改失败！'}`);
      }
    },
    // *exportPage({ payload }, { call, put }) {},
  },

  reducers: {
    handleFileNo(state, { payload }) {
      const arr = payload.split('|');
      return {
        ...state,
        fileId: arr[0],
        fileName: arr[1],
      };
    },
    // 获取工伤二级户列表
    getInjuryPageList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = data.pageRecords && Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        selectedRowKeys: [],
        selectedRows: [],
        pageData: {
          list,
          pagination: { total: data.totalCount || 0, current: currentPage, pageSize },
        },
      };
    },
    handleDetailInfo(state, { payload }) {
      let detailFormFieldsValue = {};
      let detailInfo = {};
      for (const key in payload) {
        detailInfo = Object.assign(detailInfo, {
          [key]: payload[key],
        });
        switch (key) {
          case 'injSecAcco':
            for (const tempKey in payload[key]) {
              detailFormFieldsValue = Object.assign(detailFormFieldsValue, {
                [tempKey]: payload[key][tempKey],
              });
            }
            detailInfo = Object.assign(detailInfo, {
              injSecAccoId: payload[key].injSecAccoId,
              insAccoId: payload[key].insAccoId,
              insAccoName: payload[key].insAccoName,
              state: payload[key].state,
              useTaxFlag: String(payload[key].useTaxFlag),
            });
            break;
          case 'ratioList':
            if (payload[key].length) {
              payload[key].forEach((item, i) => {
                item.rangeDate = `${item.beginYm}01`;
                item.beginDisDate = `${item.beginYm}01`;
                item.key = `list_${i + 100}`;
              });
              detailInfo = Object.assign(detailInfo, {
                ratioList: payload[key],
              });
            }
            break;
          case 'custList':
            const temp = payload[key];
            const tempArr = [];
            const tempArrList = [];
            temp.forEach(value => {
              const tempJson = {};
              tempJson.key = value.mdCustId;
              tempJson.label = value.mdCustName;
              tempArr.push(tempJson);
              tempArrList.push(value.mdCustId);
            });
            detailFormFieldsValue = Object.assign(detailFormFieldsValue, {
              [key]: tempArr,
            });
            detailInfo = Object.assign(detailInfo, {
              mdCustIdList: tempArrList,
            });
        }
      }
      return {
        ...state,
        detailInfo,
        detailFormFieldsValue,
      };
    },
    handleDetails(state, { payload }) {
      let obj = {};
      for (const key in payload) {
        if (key === 'injSecAcco') {
          obj = Object.assign(obj, {
            insAccoName: payload.injSecAcco.insAccoName,
            injSecAccoName: payload.injSecAcco.injSecAccoName,
            injSecAccoCode: payload.injSecAcco.injSecAccoCode,
            feeRatio: payload.injSecAcco.feeRatio,
            remark: payload.injSecAcco.remark,
            useTaxFlag: String(payload.injSecAcco.useTaxFlag),
          });
        }
        if (key === 'ratioList') {
          obj = Object.assign(obj, {
            ratioListDetail: payload.ratioList,
          });
        }
        if (key === 'custList') {
          const tempArr = [];
          payload.custList.forEach(item => {
            tempArr.push(item.mdCustName);
          });
          obj = Object.assign(obj, {
            custList: tempArr.join(','),
          });
        }
      }
      return {
        ...state,
        ...obj,
      };
    },
    updateEditCustName(state, { payload }) {
      const tempList = payload.filter(
        item => item.mdCustId.indexOf(state.detailInfo.mdCustIdList) === -1
      );
      return {
        ...state,
        comList: tempList,
      };
    },
    // 设置修改当前state数据
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    // 获取大库下拉列表
    getBigwareList(state, { payload }) {
      return {
        ...state,
        bigwareList: payload,
      };
    },
    // 获取工伤二级户下拉列表
    getInjuryList(state, { payload }) {
      return {
        ...state,
        injuryList: payload,
      };
    },

    //
  },
};
